home *** CD-ROM | disk | FTP | other *** search
/ Windows 6-Pak - Disc 5 / Windows 6-Pak (InfoMagic) (Disc 5) (1999).ISO / C&C++Tools / sbparser.exe / cpp5x / pars.cpp next >
Encoding:
C/C++ Source or Header  |  1998-08-26  |  8.1 KB  |  352 lines

  1. #include <owl\owlpch.h>
  2. #include <owl\applicat.h>
  3. #include <owl\dc.h>
  4. #include <owl\dialog.h>
  5. #include <owl\framewin.h>
  6. #include <cstring.h>
  7. #include <ctype.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #include <astsbpar.h> //Link necessary header
  13.  
  14. const char AppName[] = "Parser";
  15. const int ID_PARSFUNC = 500;
  16. const int ID_VARX = 600;
  17. const int ID_VARY = 700;
  18. const int ID_BDEC = 800;
  19. const int ID_BBIN = 825;
  20.  
  21. const int ID_DISPLAY = 400;
  22. const int ID_GETRESULTBTN = 61;
  23. const int ID_PARSICON = 101;
  24.  
  25. class TPars : public TDialog {
  26.     public:
  27.         TBrush      BlueBrush;
  28.  
  29.         TPars();
  30.         void            ParsResult();
  31.       void           ClickedDEC();
  32.       void           ClickedBIN();
  33.  
  34.     protected:
  35.        void             SetupWindow() {
  36.           TDialog::SetupWindow();
  37.  
  38.             CheckDlgButton(ID_BDEC,TRUE);
  39.          ::SetWindowText(GetParent(),"Demo for Borland C++ 5.x");
  40.       }
  41.         void        EvPaint();
  42.         HBRUSH      EvCtlColor(HDC, HWND hWndChild, UINT ctlType);
  43.  
  44.       void SolveParsVariables();
  45.       void SolveParsBase();
  46.  
  47.     DECLARE_RESPONSE_TABLE(TPars);
  48. };
  49.  
  50. DEFINE_RESPONSE_TABLE1(TPars, TDialog)
  51.     EV_WM_PAINT,
  52.     EV_WM_CTLCOLOR,
  53.     EV_BN_CLICKED(ID_GETRESULTBTN,ParsResult),
  54.     EV_BN_CLICKED(ID_BDEC,ClickedDEC),
  55.     EV_BN_CLICKED(ID_BBIN,ClickedBIN),
  56. END_RESPONSE_TABLE;
  57.  
  58. TPars::TPars()
  59.     : TWindow((TWindow*)0),
  60.         TDialog(0, AppName),
  61.         BlueBrush(TColor(0, 0, 255))
  62. {
  63. }
  64.  
  65. //--------------------- BEGIN: HOW TO USE --------------------------------------
  66. void
  67. TPars::ClickedDEC()
  68. {
  69.     ::EnableWindow(GetDlgItem(ID_VARX),TRUE);
  70.     ::EnableWindow(GetDlgItem(ID_VARY),TRUE);
  71. }
  72.  
  73. void
  74. TPars::ClickedBIN()
  75. {
  76.     ::EnableWindow(GetDlgItem(ID_VARX),false);
  77.     ::EnableWindow(GetDlgItem(ID_VARY),false);
  78. }
  79.  
  80. void
  81. TPars::ParsResult()
  82. {
  83.      if (IsDlgButtonChecked(ID_BDEC)==TRUE) {
  84.        SolveParsVariables();
  85.    }
  86.      if (IsDlgButtonChecked(ID_BBIN)==TRUE) {
  87.        SolveParsBase();
  88.    }
  89. }
  90.  
  91. void
  92. TPars::SolveParsVariables()
  93. {
  94.     char* functempstr;
  95.     char* xtempstr;
  96.     char* ytempstr;
  97.    char errText[150];
  98.    char tempstr[150];
  99.    long double xyval[2];
  100.    int tempi;
  101.      char *ptr;
  102.    bool err=false;
  103.  
  104.    //Get the function f(x,y)
  105.    tempi=::GetWindowTextLength(GetDlgItem(ID_PARSFUNC));
  106.    functempstr=new char [tempi+1];
  107.    ::GetWindowText(GetDlgItem(ID_PARSFUNC),functempstr,tempi+1);
  108.  
  109.    //Get the value for the variable x as a string
  110.    tempi=::GetWindowTextLength(GetDlgItem(ID_VARX));
  111.    xtempstr=new char [tempi+1];
  112.    ::GetWindowText(GetDlgItem(ID_VARX),xtempstr,tempi+1);
  113.  
  114.    //Get the value for the variable y as a string
  115.    tempi=::GetWindowTextLength(GetDlgItem(ID_VARY));
  116.    ytempstr=new char [tempi+1];
  117.    ::GetWindowText(GetDlgItem(ID_VARY),ytempstr,tempi+1);
  118.  
  119.    //Convert the strings of the variables to long double
  120.     xyval[0]=chartold(xtempstr,&ptr);
  121.    if (strlen(ptr))
  122.    {
  123.       ::MessageBox(HWindow,
  124.          "The string of the variable x cannot be converted to long double.",
  125.          "Error",
  126.          MB_ICONERROR);
  127.       err=true;
  128.    }
  129.     xyval[1]=chartold(ytempstr,&ptr);
  130.    if (strlen(ptr))
  131.    {
  132.       ::MessageBox(HWindow,
  133.          "The string of the variable y cannot be converted to long double.",
  134.          "Error",
  135.          MB_ICONERROR);
  136.       err=true;
  137.    }
  138.  
  139.    if (err==false)
  140.    if (strlen(functempstr)>0)
  141.    {
  142.  
  143.       //Create an sbParser object and receive its HANDLE
  144.       HANDLE hParser;
  145.       hParser=    CreateNewParser(functempstr,2,"xy");
  146.  
  147.       long double Result;
  148.  
  149.       if (GetIsError(hParser))
  150.       {
  151.          //An error has occurred
  152.          ::MessageBox(HWindow,
  153.              ldtochar(errText,GetGlobalError(hParser)),"Errornumber in Function",
  154.             MB_ICONERROR);
  155.  
  156.          Result=    0;
  157.          err= true;
  158.       }
  159.       else
  160.       {
  161.           //Compute the result
  162.          Result=    GetResultExt(hParser,xyval);
  163.  
  164.          if (GetIsError(hParser))
  165.          {
  166.              //An error has occurred
  167.             ::MessageBox(HWindow,
  168.                ldtochar(errText,GetGlobalError(hParser)),
  169.                "Errornumber in Calculation",MB_ICONERROR);
  170.  
  171.             Result=    0;
  172.             err= true;
  173.          }
  174.          else
  175.          {
  176.                 //Convert the long double to a string
  177.             ldtochar(tempstr,Result,10,true);
  178.  
  179.             ::SetWindowText(GetDlgItem(ID_DISPLAY),tempstr);
  180.          }
  181.  
  182.       }
  183.  
  184.       //Delete of the sbParser object
  185.       DeleteParser(hParser);
  186.    }
  187.    else
  188.    {
  189.       ::MessageBox(HWindow,"There is no function to compute.","Error",
  190.          MB_ICONERROR);
  191.    }
  192.  
  193.    if (err)
  194.    {
  195.        ::SetWindowText(GetDlgItem(ID_DISPLAY),"Error");
  196.    }
  197.  
  198.     delete[] functempstr;
  199.     delete[] xtempstr;
  200.     delete[] ytempstr;
  201. }
  202.  
  203. void
  204. TPars::SolveParsBase()
  205. {
  206.     char* functempstr;
  207.    char errText[150];
  208.    char tempstr[150];
  209.    int tempi;
  210.    bool err=false;
  211.  
  212.    //Get the function f(x,y)
  213.    tempi=::GetWindowTextLength(GetDlgItem(ID_PARSFUNC));
  214.    functempstr=new char [tempi+1];
  215.    ::GetWindowText(GetDlgItem(ID_PARSFUNC),functempstr,tempi+1);
  216.  
  217.    if (strlen(functempstr)>0)
  218.    {
  219.  
  220.       //Create an sbParser object and receive its HANDLE
  221.       HANDLE hParser;
  222.       hParser=    CreateNewParser(functempstr,2);
  223.  
  224.       long double Result;
  225.  
  226.       if (GetIsError(hParser))
  227.       {
  228.          //An error has occurred
  229.          ::MessageBox(HWindow,
  230.              ldtochar(errText,GetGlobalError(hParser)),"Errornumber in Function",
  231.             MB_ICONERROR);
  232.  
  233.          Result=    0;
  234.       }
  235.       else
  236.       {
  237.           //Compute the result
  238.          Result=    GetResult(hParser);
  239.  
  240.          if (GetIsError(hParser))
  241.          {
  242.              //An error has occurred
  243.             ::MessageBox(HWindow,
  244.                ldtochar(errText,GetGlobalError(hParser)),
  245.                "Errornumber in Calculation",MB_ICONERROR);
  246.  
  247.             Result=    0;
  248.             err= true;
  249.          }
  250.          else
  251.          {
  252.                 //Convert the long double to a string for base binary
  253.             ConvertToBase(tempstr,Result,10,2);
  254.  
  255.             ::SetWindowText(GetDlgItem(ID_DISPLAY),tempstr);
  256.          }
  257.  
  258.       }
  259.  
  260.       //Delete of the sbParser object
  261.       DeleteParser(hParser);
  262.    }
  263.    else
  264.    {
  265.       ::MessageBox(HWindow,"There is no function to compute.","Error",
  266.          MB_ICONERROR);
  267.    }
  268.  
  269.    if (err)
  270.    {
  271.        ::SetWindowText(GetDlgItem(ID_DISPLAY),"Error");
  272.    }
  273.  
  274.     delete[] functempstr;
  275. }
  276. //--------------------- END: HOW TO USE ----------------------------------------
  277.  
  278. HBRUSH
  279. TPars::EvCtlColor(HDC hDC, HWND hWndChild, UINT ctlType)
  280. {
  281.       TBrush redBrush(TColor(255, 0, 0));
  282.  
  283.     switch (ctlType) {
  284.         case CTLCOLOR_BTN:
  285.             SetBkMode(hDC, TRANSPARENT);
  286.             return (HBRUSH)GetStockObject(NULL_BRUSH);
  287.  
  288.         case CTLCOLOR_STATIC:
  289.             SetTextColor(hDC, TColor::LtYellow);
  290.             SetBkMode(hDC, TRANSPARENT);
  291.             return (HBRUSH)redBrush;
  292.  
  293.         case CTLCOLOR_DLG:
  294.             SetBkMode(hDC, TRANSPARENT);
  295.             return (HBRUSH)BlueBrush;
  296.  
  297.         default:
  298.             return TDialog::EvCtlColor(hDC, hWndChild, ctlType);
  299.     }
  300. }
  301.  
  302. void
  303. TPars::EvPaint()
  304. {
  305.     TBrush    redBrush(TColor(255, 0, 0));
  306.     TPaintDC  dc(*this);
  307.  
  308.     dc.SelectObject(redBrush);
  309.     dc.SelectStockObject(NULL_PEN);
  310.  
  311.     TRect clientRect = GetClientRect();
  312.     clientRect.bottom = clientRect.right;
  313.     clientRect.Offset(-clientRect.right/4, -clientRect.right/4);
  314.     dc.Ellipse(clientRect);
  315. }
  316.  
  317. //------------------------------------------------------------------------------
  318.  
  319. class TParsApp : public TApplication {
  320.     public:
  321.         //--------------------- BEGIN: HOW TO USE II -----------------------------
  322.         TParsApp(const char far* name) : TApplication(name) {
  323.          //Loading of the csbparse.DLL
  324.          InitSTDsbParserDLL();
  325.       }
  326.       ~TParsApp() {
  327.          //Unloading of the csbparse.DLL
  328.          DeinitSTDsbParserDLL();
  329.       }
  330.         //--------------------- END: HOW TO USE II -------------------------------
  331.  
  332.         void   InitMainWindow();
  333. };
  334.  
  335. void
  336. TParsApp::InitMainWindow()
  337. {
  338.     TWindow* parsWin = new TPars;
  339.     parsWin->Attr.AccelTable = AppName;
  340.  
  341.     MainWindow = new TFrameWindow(0, Name, parsWin, TRUE);
  342.     MainWindow->SetIcon(this, ID_PARSICON);
  343.    MainWindow->SetIconSm(this, ID_PARSICON);
  344.     MainWindow->Attr.Style &= ~(WS_MAXIMIZEBOX | WS_THICKFRAME);
  345. }
  346.  
  347. int
  348. OwlMain(int /*argc*/, char* /*argv*/ [])
  349. {
  350.     return TParsApp(AppName).Run();
  351. }
  352.